package com.awesomeproject;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.nineoldandroids.view.ViewHelper;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/12/10.
 * 首页activity
 */

public class HomeActivity extends MyBaseActivity{
    @BindView(R.id.id_drawerLayout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.myToolbar)
    Toolbar mToolbar;
    @BindView(R.id.item_recyclerView)
    SuperRecyclerView recyclerView;
    private List<String> tempList;
    List<String> list;

    @Override
    public int getViewId() {
        return R.layout.activity_home;
    }

    @Override
    protected void initVariables() {
        list=new ArrayList<>();
        list=initData(10);
        tempList = new ArrayList<>();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initToolbar();
        initDrawerLayout();
        initRecyclerView();
    }

    @Override
    protected void loadData() {

    }

    //Toolbar显示菜单
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.toolbar_home_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    //Toolbar菜单点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.plus:{
            }break;
            case R.id.check:{
            }break;
            case R.id.query:{
            }break;
            default:Toast.makeText(HomeActivity.this,"菜单点击",Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    //右边菜单
    public void left_menu_click(View view)
    {
        switch (view.getId())
        {
            case R.id.left_menu_click1:{
                Toast.makeText(HomeActivity.this,"个人资料",Toast.LENGTH_SHORT).show();
            }break;
            default:Toast.makeText(HomeActivity.this, "sa",Toast.LENGTH_SHORT).show();
        }
    }


    private void initToolbar() {
        mToolbar.setTitle("Call酒么");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void initDrawerLayout() {
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
                Gravity.RIGHT);

        mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener()
        {
            @Override
            public void onDrawerStateChanged(int newState)
            {
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset)
            {
                View mContent = mDrawerLayout.getChildAt(0);
                View mMenu = drawerView;
                float scale = 1 - slideOffset;
                float rightScale = 0.8f + scale * 0.2f;

                if (drawerView.getTag().equals("LEFT"))
                {

                    float leftScale = 1 - 0.3f * scale;

                    ViewHelper.setScaleX(mMenu, leftScale);
                    ViewHelper.setScaleY(mMenu, leftScale);
                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
                    ViewHelper.setTranslationX(mContent,
                            mMenu.getMeasuredWidth() * (1 - scale));
                    ViewHelper.setPivotX(mContent, 0);
                    ViewHelper.setPivotY(mContent,
                            mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                }

            }
            @Override
            public void onDrawerOpened(View drawerView)
            {
            }

            @Override
            public void onDrawerClosed(View drawerView)
            {
                mDrawerLayout.setDrawerLockMode(
                        DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
            }
        });

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        actionBarDrawerToggle.syncState();
        mDrawerLayout.addDrawerListener(actionBarDrawerToggle);
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        for (int i=0;i<10;i++) {
            list.add("100块");
        }
        final MainAdapter adapter=new MainAdapter(HomeActivity.this,list);
        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshEnabled(true);//可以定制是否开启下拉刷新
        recyclerView.setLoadMoreEnabled(true);//可以定制是否开启加载更多
//        recyclerView.addItemDecoration(new RecycleViewDivider(
//                this, LinearLayoutManager.VERTICAL, R.drawable.divider_mileage));
        recyclerView.setLoadingListener(new SuperRecyclerView.LoadingListener(){

            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list = initData(1);
                        recyclerView.completeRefresh();
                        adapter.notifyDataSetChanged();
                    }
                }, 3000);
            }

            @Override
            public void onLoadMore() {
                if(list.size() >= 1){
                    recyclerView.setNoMore(true);
                    return;
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tempList.clear();
                        tempList = getDataList(20);
                        list.addAll(tempList);
                        recyclerView.completeLoadMore();
                    }
                }, 3000);
            }
        });
    }

    private List<String> initData(int size) {
        list.clear();
        for (int i = 1; i <= size; i++) {
            list.add("数据" + i);
        }
        return list;
    }

    private List<String> getDataList(int size) {
        List<String> data = new ArrayList<>();
        for (int i = 1; i <= size; i++) {
            data.add("新加的数据" + i);
        }
        return data;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        ViewPager
        if (keyCode==KeyEvent.KEYCODE_BACK)
        {
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
